import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syria_sports/data/controllers/subscriptions_controller.dart';

import '../../../../core/utils/show_toast.dart';
import '../models/subscription.dart';

part 'subscriptions_state.dart';

class SubscriptionsCubit extends Cubit<SubscriptionsState> {
  SubscriptionsCubit() : super(SubscriptionsInitial());

  static SubscriptionsCubit get(context) => BlocProvider.of(context);

  List<Subscription> subscriptions = [];

  int? type = 1;
  void init() {
    getAllSubscriptions();
  }

  Future<void> getAllSubscriptions()async{
    subscriptions =[];
    emit(SubscriptionsLoading());
    await SubscriptionsController.getAllSubscriptions(type: type).catchError((e){
      emit(SubscriptionsError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        subscriptions = List<Subscription>.generate(value.data.length, (index) => Subscription.fromJson(value.data[index]));
        emit(SubscriptionsDone());
      }else if(value != null){
        showMessage(value.message);
        emit(SubscriptionsError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
        emit(SubscriptionsError());
      }
    });

  }



}
